import { ExpressionSpecification } from 'mapbox-gl';

/**
 * 用于构建 match 表达式
 * 可用在 paint 和 layout 中
 */
export class MatchBuilder {
  private property: string;
  private cases: ([string | number, any] | [any])[] = [];
  constructor(property: string) {
    this.property = property;
  }
  static match(property: string) {
    return new MatchBuilder(property);
  }

  case(when: string | number, value: any): MatchBuilder {
    this.cases.push([when, value]);
    return this;
  }

  default(value: any) {
    this.cases.push([value]);
    return this;
  }

  format(): ExpressionSpecification {
    const cases = this.cases.reduce((acc, caseContent) => {
      acc.push(...caseContent);
      return acc;
    }, [] as any[]);
    return ['match', ['get', this.property], ...cases];
  }
}
